{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "35ab0a69",
   "metadata": {},
   "source": [
    "# week02\n",
    "* 申请github education pack\n",
    "* 下载注册pycharm专业版\n",
    "* 集成开发环境IDE，是用于提供程序开发环境的应用程序；而IDLE是提供了一个功能完备的代码编辑器，还可在Python Shell中试验运行代码。\n",
    "* 认识什么是jupyter notebook\n",
    "* jupyter\n",
    "* jupyter是一个基于web的IDE,能够兼具脚本操作和交互式操作的特性，笔记式编辑代码和运行，便于调试，便于保存。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e77c882a",
   "metadata": {},
   "source": [
    "## 变量\n",
    "* 是看不见摸不着的，是信息是数据，真实存在却是虚拟的 *常用“=”将一些东西装到变量中，如：name =\"hj\"\n",
    "所表示的是一个变量，变量名为：name,变量name的值为：“hj”\n",
    "* 变量定义的规则：\n",
    "变量名只能是数字、字母或者下划线\n",
    "变量名的第一个字符不能是数字\n",
    "还有以下关键字不能声明为变量名：['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda','not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']\n",
    "\n",
    "* 变量的赋值:原则是从上到下\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6fe3e8d5",
   "metadata": {},
   "source": [
    "## 理解赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e9611e9c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "odds = [ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19,\n",
    "       21, 23, 25, 27, 29, 31, 33, 35, 37, 39]\n",
    "#变量名为“odds”,为其赋予一个整数列表\n",
    "\n",
    "right_this_minute = datetime.today().minute \n",
    "#“right_this_minute”为一个变量，而“datetime.today().minute”是一个赋值，整句代码为将这个值赋给这个变量\n",
    "#“right_this_minute”表示抽出右边变量中的分钟（minute）\n",
    "\n",
    "if right_this_minute in odds:\n",
    "    print(\"a\")\n",
    "else:\n",
    "    print(\"b\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2adf888a",
   "metadata": {},
   "source": [
    "## if,else"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ea0488f5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Not an odd minute.\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "odds = [ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19,\n",
    "       21, 23, 25, 27, 29, 31, 33, 35, 37, 39]\n",
    "\n",
    "right_this_minute = datetime.today().minute \n",
    "\n",
    "if right_this_minute in odds: #\"in\"能确定一个对象是否存在于另一个对象中\n",
    "    print(\"This minute seems a little odd.\") #符合条件则出现\n",
    "else: #冒号引入一个缩进的代码组（如：if，else，for）\n",
    "    print(\"Not an odd minute.\") #不符合条件时出现"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c3ce0b5",
   "metadata": {},
   "source": [
    "注： if语句 if 条件: if-语句块\n",
    "    else:\n",
    "    else-语句块\n",
    "    if 条件： if-语句块 elif 条件: elif-语句块\n",
    "    ...\n",
    "    else:\n",
    "    else-语句块\n",
    "    可以进行嵌套。 不要超过3层， 最多5层"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fabcabf7",
   "metadata": {},
   "source": [
    "## 循环嵌套 练习if elif\n",
    "* “else if”拼为“elif"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "299be118",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'today' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-3-5bc4aaa89c26>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mif\u001b[0m \u001b[0mtoday\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'Saturday'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Party!'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;32melif\u001b[0m \u001b[0mtoday\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'Sunday'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mcondition\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'Headache'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m         \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Recover, then rest.'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'today' is not defined"
     ]
    }
   ],
   "source": [
    "if today == 'Saturday':\n",
    "    print('Party!')\n",
    "elif today == 'Sunday':\n",
    "    if condition == 'Headache':\n",
    "        print('Recover, then rest.')\n",
    "    else:\n",
    "        print('Rest.')\n",
    "else:\n",
    "    print('Work')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a4451da",
   "metadata": {},
   "source": [
    "## for循环\n",
    "* 知道需要进行多少次迭代时用for循环合适，如果不知道时（有条件限制时）用while循环更合适。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "be29a4c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "#for循环取一个数字列表，迭代处理列表中的每一个数字，在显示屏上显示数字。i为迭代变量名\n",
    ">>> for i in [1, 2, 3]:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "04ff649a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "n\n",
      "i\n",
      "c\n",
      "e\n",
      "!\n"
     ]
    }
   ],
   "source": [
    "#for循环迭代处理一个字符串，每次迭代时处理字符串中的一个字符\n",
    ">>> for ch in \"nice!\": #处理完序列中的所有对象时就会终止for循环\n",
    "    print(ch) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0bdb320e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "a\n",
      "a\n",
      "a\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "#优化添加for循环行\n",
    "from datetime import datetime\n",
    "\n",
    "odds = [ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19,\n",
    "       21, 23, 25, 27, 29, 31, 33, 35, 37, 39]\n",
    "\n",
    "for i in range(5):\n",
    "    right_this_minute = datetime.today().minute \n",
    "    \n",
    "    if right_this_minute in odds:\n",
    "        print(\"a\")\n",
    "    else:\n",
    "        print(\"b\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38de50ca",
   "metadata": {},
   "source": [
    "## 内置函数range\n",
    "* 函数range可以指定迭代次数，指示for循环要运行多少次"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7c8bdb6c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "happy !\n",
      "happy !\n",
      "happy !\n",
      "happy !\n",
      "happy !\n",
      "happy !\n"
     ]
    }
   ],
   "source": [
    ">>> for num in range(6):\n",
    "    print('happy !')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "354101fa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "#迭代一系列数字\n",
    ">>> for i in range(6):\n",
    "     print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e96bd8fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[5, 7, 9]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(5,10,2)) #（从5开始，在10之内，指定增量为2）即（开始，范围，增量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4062f2f2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
